package chat;

import javax.print.attribute.standard.Severity;
import javax.swing.JPanel;

import java.awt.BorderLayout;

import javax.swing.JTextField;

import java.awt.Color;

import javax.swing.JButton;
import javax.swing.JProgressBar;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.JScrollPane;
import javax.swing.text.JTextComponent;

import java.awt.Rectangle;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JLabel;

public class ChatingSever extends JPanel implements Runnable{
	
	JTextArea textArea_1;
	ServerSocket serverSocket= null;
	boolean acceptFlag = true;
	
	
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_4;
	private JTextField textField_5;

	/**
	 * Create the panel.
	 */
	public ChatingSever() {
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		panel.setLayout(null);
		add(panel, BorderLayout.CENTER);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(76, 48, 145, 202);
		panel.add(scrollPane);
		
		JTextArea textArea = new JTextArea();
		scrollPane.setViewportView(textArea);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(246, 48, 377, 202);
		panel.add(scrollPane_1);
		
		textArea_1 = new JTextArea();
		scrollPane_1.setViewportView(textArea_1);
		
		JPanel panel_1 = new JPanel();
		add(panel_1, BorderLayout.NORTH);
		
		JLabel lblIp = new JLabel("서버 IP");
		panel_1.add(lblIp);
		
		textField = new JTextField();
		textField.setText("IP");
		textField.setHorizontalAlignment(SwingConstants.CENTER);
		textField.setColumns(10);
		textField.setBackground(Color.GREEN);
		panel_1.add(textField);
		
		JLabel lblTj = new JLabel("서버 Port");
		panel_1.add(lblTj);
		
		textField_1 = new JTextField();
		textField_1.setText("Port");
		textField_1.setHorizontalAlignment(SwingConstants.CENTER);
		textField_1.setColumns(10);
		textField_1.setBackground(Color.GREEN);
		panel_1.add(textField_1);
		
		JButton button_1 = new JButton("서버시작");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				Thread t = new Thread(ChatingSever.this);
				t.start();
			}		
		});
		panel_1.add(button_1);
		
		JButton button = new JButton("서버중지");
		panel_1.add(button);
		
		JPanel panel_2 = new JPanel();
		add(panel_2, BorderLayout.SOUTH);
		panel_2.setLayout(new BorderLayout(0, 0));
		
		textField_4 = new JTextField();
		textField_4.setBounds(new Rectangle(0, 0, 500, 0));
		textField_4.setColumns(10);
		panel_2.add(textField_4, BorderLayout.CENTER);
		
		textField_5 = new JTextField();
		textField_5.setText("이모티콘");
		textField_5.setHorizontalAlignment(SwingConstants.CENTER);
		textField_5.setColumns(10);
		textField_5.setBackground(Color.GREEN);
		panel_2.add(textField_5, BorderLayout.WEST);
		
		JButton button_2 = new JButton("전송");
		panel_2.add(button_2, BorderLayout.EAST);
		
		
		

		
	}

	@Override
	public void run() {
		// 서버 주소를 자동으로 검색하여 ServerIp에 설정
				try{
					InetAddress localhost = InetAddress.getLocalHost();
					textField.setText(localhost.getHostAddress());
					
					serverSocket = new ServerSocket(7777);
					
					while(acceptFlag){
						Socket client = serverSocket.accept();
						
						textArea_1.append("어서와"+"\n");
						
						
						OutputStream os = client.getOutputStream();
						ObjectOutputStream oos  = new ObjectOutputStream(os);
						
						InputStream is = client.getInputStream();
						ObjectInputStream ois = new ObjectInputStream(is);
						
						chatData cd = new chatData();
						cd.setMessage("서버에 접속되었습니다.");
						
						oos.writeObject(cd);
						oos.flush();
						
					}
					
					
				}catch(Exception ex){
					
				}
		
	}
}
